package net.fiodor.utils {

	/**
	 * @author Tomasz Swierszcz, 
	 * email: tomasz.swierszcz@dreamlab.pl
	 */
	public class MathUtils {

		
		public static function sgn( n : Number ) : Number {
		
			return  ( ( n == 0 ) ? 0 : ( Math.abs(n) / n ) );	
		}

		public static function roundTo( n : Number, places : Number ) : Number {
		
			places = places || 0;
			var m : Number = Math.pow(10, places);
			return Math.round(n * m) / m;	
		}

		public static function zeroFill( n : Number, places : Number, trailing : Boolean ) : String {
		
			var filledVar : String = String(n);
			var len : Number = filledVar.length;
			for( var i : Number = 0;i < ( places - len ); i++ ) 	filledVar = trailing ? '0' + filledVar : filledVar + '0';	
			
			return filledVar;
		}

		public static function randRange( min : Number, max : Number, places : Number ) : Number {
		
			places = places || 0;
			var rangeDiff : Number = ( max - min ) * Math.pow(10, places) + 1;
			var randVal : Number = Math.floor(Math.random() * rangeDiff);
			randVal /= Math.pow(10, places);
			randVal += min;
			return MathUtils.roundTo(randVal, Math.pow(10, -places));
		}

		public static function rad2deg( rad : Number ) : Number {
		
			return ( 180 * rad ) / Math.PI;
		}

		public static function deg2rad( deg : Number ) : Number {
		
			return ( Math.PI * deg ) / 180;
		}

		public static function NWD( a : Number, b : Number ) : Number {
		
			var	x : Number = Math.max(a, b);
			var y : Number = Math.min(a, b);
			var r : Number = y;

			while (x % y != 0) {
			
				r = x % y;
				x = y;
				y = r;
			}

			return r;
		}

		public static function NWW( a : Number, b : Number ) : Number {
		
			return a * b / NWD(a, b);
		}
	}
}